package com.quanyan.orderpay.web.admin;

import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.utils.ValidationResult;
import com.quanyan.common.utils.ValidationUtils;
import com.quanyan.order.entity.Order;
import com.quanyan.order.vo.PlatformOrderQueryPageObj;
import com.quanyan.order.vo.PlatformOrderQueryRespVo;
import com.quanyan.orderpay.service.AdminOrderService;
import com.quanyan.order.entity.PlatformOrderQueryParamsVo;
import com.quanyan.orderpay.vo.request.QueryOrderParamsVo;
import com.quanyan.orderpay.vo.request.QueryRefundParamsVo;
import com.quanyan.orderpay.vo.request.UpdateRefundStatusParamsVo;
import com.quanyan.pay.entity.RefundRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Zheng.Ke
 * Date 2016/8/15.
 */
@RestController
@RequestMapping("/admin/api/order")
public class AdminOrderController {
    @Autowired
    private AdminOrderService adminOrderService;


    @RequestMapping(value = "/orderList", method = RequestMethod.POST, produces="application/json")
    public APIResponse<PageObj<List<Order>>> orderList(@RequestBody QueryOrderParamsVo paramsVo) {
        return adminOrderService.queryOrder(paramsVo);
    }

    @RequestMapping(value = "/refundList", method = RequestMethod.POST, produces="application/json")
    public APIResponse<PageObj<List<RefundRecord>>> orderList(@RequestBody QueryRefundParamsVo paramsVo) {
        return adminOrderService.refundList(paramsVo);
    }

    @RequestMapping(value = "/updateRefundStatus", method = RequestMethod.POST, produces="application/json")
    public APIResponse<RefundRecord> updateRefundStatus(@RequestBody @Valid UpdateRefundStatusParamsVo paramsVo, BindingResult result) {
        if (result.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID, result.getFieldError().getField() + "" + result.getFieldError().getDefaultMessage());
        }
        return adminOrderService.updateRefundStatus(paramsVo);
    }

    @RequestMapping(value = "/platformOrderQueryList", method = RequestMethod.POST, produces="application/json")
    public APIResponse<PlatformOrderQueryPageObj<List<PlatformOrderQueryRespVo>>> platformOrderQueryList(@RequestBody PlatformOrderQueryParamsVo paramsVo) {
        ValidationResult result = ValidationUtils.validateEntity(paramsVo);
        if (result.hasErrors()) {
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_INVALID, result.getErrorDesc());
        }
        return adminOrderService.platformOrderQueryList(paramsVo);
    }

}
